/*
 * @Description: 修改任务
 * @Author: Zzt
 * @Date: 2022-08-09 14:59:01
 * @LastEditTime: 2022-08-09 16:17:06
 */

import { Button, Form, Modal } from 'antd';
import { useEffect } from 'react';
import { useTaskCronEContext } from '../../contexts/TaskCronEContext';
import FormItem from './common/FormItem';
const EditPlan = () => {
  const [form] = Form.useForm();
  const { choice, EditPlan, isVisible, setVisible } = useTaskCronEContext();
  const { taskname, crone, state, remark, id } = choice;
  // console.log(taskname, crone, state, remark, val);
  const onFinish = (values: any) => {
    values.id = id;
    // values.state = state;
    console.log(values);
    EditPlan(values);
    setVisible(false);
  };

  const onFinishFailed = (errorInfo: any) => {
    console.log('Failed:', errorInfo);
  };

  const handleOk = () => {
    form.submit();
  };

  const handleCancel = () => {
    setVisible(false);
  };
  useEffect(() => {
    if (isVisible) {
      if (choice?.id) {
        form.setFieldsValue(choice);
      }
    }
  }, [isVisible]);
  return (
    <div>
      <Modal
        title="修改任务"
        visible={isVisible}
        onOk={handleOk}
        onCancel={handleCancel}
        footer={[
          <Button key="back" onClick={handleCancel}>
            取消
          </Button>,
          <Button key="submit" type="primary" onClick={handleOk}>
            保存
          </Button>
        ]}
      >
        <Form
          form={form}
          labelCol={{ span: 5 }}
          wrapperCol={{ span: 20 }}
          onFinish={onFinish}
          onFinishFailed={onFinishFailed}
          // eslint-disable-next-line object-shorthand
          initialValues={{ taskname: taskname, state: state, remark: remark, crone: crone }}
        >
          <FormItem />
        </Form>
      </Modal>
    </div>
  );
};
export default EditPlan;
